package com.student_exam_system.dao;

import com.student_exam_system.pojo.Answer;
import com.student_exam_system.pojo.AnswerCards;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author fire'ge
 */
public interface AnswerMapper {
    /**添加一个Answer
     * @param answer 传入answer类
     * @return int
     */
    int addAnswer(Answer answer);

    /**添加多个Answer
     * @param answer 传入answer类
     * @return int
     */
    int addMulAnswer(List<Answer> answer);

    /**根据id删除一个Answer
     * @param id 传入id
     * @return int
     */
    int deleteAnswerById(int id);
    
    /**更新Answer
     * @param answer 传入answer类
     * @return int
     */
    int updateAnswer(Answer answer);
    /**根据id查询,返回一个Answer
     * @param id 传入id
     * @return int
     */
    Answer queryAnswerById(int id);
    /**查询全部Answer,返回list集合
     * @return List<Answer>
     */
    List<Answer> queryAllAnswer();
    /**根据userId和paperId查询AnswerCards,返回list集合
     * @param userId 用户的id
     * @param paperId 试卷的id
     * @return List<Answer>
     */
    List<AnswerCards> queryAnswerCard(@Param("userId") int userId,@Param("paperId") int paperId);

    /**
     * 修改改考生的整张试卷是否已经评分了
     * @param paperId 试卷编号id
     * @return int 是否评分
     */
    @Update("update exam.user_paper set up_teacherAnalysis_status = 1 where up_paper = #{paperId}")
    int updateUserPaperAnalysis(int paperId);



    /**
     * 【主要任务就是遍历出所有的该张试卷的试题的所有考生的答案】
     * 评卷是个事务:[ 1、先显示考生的答案  2、教师给分，update
     *             3、当某考生的试卷的所有试题全评好分了就会再执行update user_paper 中的该张试卷释放已经被评分了 ]
     * 教师评卷【分为：自动评卷、手动评卷】
     * @param paperId 试卷id
     * @param teacherId 教师id
     * @param questionId 试题id
     * @return 是否对这张试卷这道题评分成功
     */

    @Select(" select pq.studentId,pq.questionId,pq.paperId,pq.questionType,pq.answerAnswer from exam.p_question pq,exam.s_t st" +
            "        where pq.studentId=st.studentId and st.teacherId= #{teacherId} and pq.questionId = #{questionId} and pq.answerPoint=0")
    @Results({
            @Result(column = "studentId",property = "answerUser"),
            @Result(column = "questionId",property = "question.questionId"),
            @Result(column = "pq_id",property = "answerPaperQuestion"),
            @Result(column = "paperId",property = "paper.paperId"),
            @Result(column = "questionType",property = "question.questionType"),
            @Result(column = "answerAnswer",property = "answerAnswer"),
    })
    List<Answer> markQuestion(@Param("paperId") int paperId,@Param("teacherId") int teacherId,@Param("questionId") int questionId);


    /**
     *【教师给分 】
     * @param paperId  试卷id
     * @param studentId  学生id
     * @param questionId  试题id
     * @param point 给的分数
     * @return 是否给分
     */
   @Update("update exam.givePoint set answer_point= #{point} " +
            "where studentId = #{studentId} and questionId=#{questionId} and paperId=#{paperId}")
    int givePoint(@Param("paperId") int paperId,@Param("studentId") int studentId,@Param("questionId") int questionId,@Param("point") double point);


    /**
     * 修改考生这道题已经改过的状态
     * @param paperId  paper的id
     * @param questionId 试题id
     * @param studentId  学生id
     * @return 1：修改过了   0：还未改
     */
    @Update("update exam.givePoint set questionAnalysisStatus = 1 where paper_question = #{paper_question} and studentId = #{studentId}")
    int updateQaStatus(@Param("paperId") int paperId, @Param("questionId") int questionId,@Param("studentId") int studentId);


    /**
     *  查看考生这张答卷是否被改完了
     * @param studentId 学生id
     * @param paperId  试卷id
     * @return  list集合的该考生的该试卷的每道题是否已经被改的状态数字
     */
    @Select("select questionAnalysisStatus from exam.givePoint where studentId=#{studentId} and paperId= #{paperId}")
    List<Integer> selectPsQ(@Param("studentId") int studentId,@Param("paperId") int paperId);


    /**
     * 查找标准答案
     * @param paperId 试卷id
     * @param questionId 试题id
     * @return 标准答案
     */
    @Select("select question_answer from exam.question q,exam.paper_question pq " +
            "where question_id = #{questionId} and pq.pq_question=q.question_id and pq.pq_paper= #{paperId}")
    String getSolution(@Param("paperId") int paperId,@Param("questionId") int questionId);

    /**
     * 获取考生的答案
     * @param paperId  试卷id
     * @param studentId  学生id
     * @param questionId  试题id
     * @return  考生答案
     */
    @Select("select a.answer_answer from exam.answer a,exam.paper_question pq " +
            "where a.answer_pq =pq.pq_id and a.answer_user = #{studentId} and pq.pq_paper = #{paperId} and pq.pq_question =#{questionId}")
    String getAnswer(@Param("paperId") int paperId,@Param("studentId")int studentId,@Param("questionId") int questionId);

}
